using System;
using System.Data;

namespace Prologis.ShifterNT {


    public class StringArrayBuilder {


        /// <summary>
        /// 
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        /// 
        public static string[] GetArray(IDataReader reader) {
            string[] array = new string[reader.FieldCount];
            for (int i = 0; i < reader.FieldCount; i++ ) {
                array[i] = HandleNullValues(reader.GetFieldType(i), reader.GetValue(i)).ToString();
            }
            return array;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        /// 
        public static object HandleNullValues(Type type, object value)
        {
            if (value != DBNull.Value)
            {
                return value;
            }

            else if (type == typeof(System.Int16)) return (Int16)0;
            else if (type == typeof(System.Int32)) return (Int32)0;
            else if (type == typeof(System.Int64)) return (Int64)0;
            else if (type == typeof(System.Byte)) return (byte)0;
            else if (type == typeof(System.Single)) return (Single)0;
            else if (type == typeof(System.Decimal)) return (decimal)0;
            else if (type == typeof(System.Double)) return (double)0;
            else if (type == typeof(System.String)) return String.Empty;
            else if (type == typeof(System.Boolean)) return false;
            else if (type == typeof(System.DateTime)) return new DateTime(1782, 5, 31);
            else if (type == typeof(System.Byte[])) return new Byte[] { };

            throw new Exception("TypeNotSupported: " + type.FullName);
        }


    }
}


 